<template>
  <a-spin :spinning="confirmLoading">
    <j-form-container :disabled="formDisabled">
      <a-form-model ref="form" :model="model" :rules="validatorRules" slot="detail">
        <a-row>
<!--          <a-col :span="12">-->
<!--            <a-form-model-item label="用户ID" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="userId">-->
<!--              <a-input v-model="model.userId" placeholder="请输入用户ID"  ></a-input>-->
<!--            </a-form-model-item>-->
<!--          </a-col>-->

<!--            <a-col :span="16" >-->
<!--              <a-form-model-item labelAlign="left"  label="身份类型" :labelCol="labelCol" :wrapperCol="wrapperCol"  prop="identityType">-->
<!--                <j-dict-select-tag      type="list" v-model="model.identityType"    dictCode="user_card"   />-->
<!--              </a-form-model-item>-->
<!--            </a-col>-->
            <a-col :span="12" >
              <a-form-model-item label="本人头像" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="realAvatar">
                <j-image-upload   v-model="model.realAvatar" ></j-image-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="12" >
              <a-form-model-item label="身份证正面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cardFront">
                <j-image-upload   v-model="model.cardFront" ></j-image-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="身份证反面" :labelCol="labelCol" :wrapperCol="wrapperCol" prop="cardOpposite">
                <j-image-upload   v-model="model.cardOpposite" ></j-image-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="个体门头" :labelCol="labelCol"  v-if="identityType ==2" :wrapperCol="wrapperCol" prop="individualDoorstep">
                <j-image-upload isMultiple  v-model="model.individualDoorstep" ></j-image-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="营业执照" :labelCol="labelCol" :wrapperCol="wrapperCol"  v-if="identityType ==3  || identityType ==2"  prop="businessLicense">
                <j-image-upload isMultiple  v-model="model.businessLicense" ></j-image-upload>
              </a-form-model-item>
            </a-col>
            <a-col :span="12">
              <a-form-model-item label="企业logo" :labelCol="labelCol" :wrapperCol="wrapperCol" v-if="identityType ==3"  prop="enterpriseLogo">
                <j-image-upload isMultiple  v-model="model.enterpriseLogo" ></j-image-upload>
              </a-form-model-item>
            </a-col>
        </a-row>
      </a-form-model>
    </j-form-container>
  </a-spin>
</template>

<script>

  import { httpAction, getAction } from '@/api/manage'
  import { validateDuplicateValue } from '@/utils/util'
  import {getIdentityData} from "@api/identity/identity";
  import {getUserById} from "@api/api"

  export default {
    name: 'SysUserIdentityForm',
    components: {
    },

    props: {
      //表单禁用
      disabled: {
        type: Boolean,
        default: false,
        required: false
      },
        userId: {
          type: String,
          default: null
        },
    },
    data () {
      return {

        model:{
          userId:null,
         },
        labelCol: {
          xs: { span: 24 },
          sm: { span: 5 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        identityType:null,
        confirmLoading: false,

        validatorRules: {
          // identityType:[{required: true, message: '请选择身份类型!'}],
          realAvatar:[{required: true, message: '请上传本人头像!'}],
          cardFront:[{required: true, message: '请上传身份证正面照片!'}],
          cardOpposite:[{required: true, message: '请上传身份证反面照片!'}],
          individualDoorstep:[{required: true, message: '请上传个体户门户照片!'}],
          businessLicense:[{required: true, message: '请上传营业执照照片!'}],
          enterpriseLogo:[{required: true, message: '请上传企业logo照片!'}],

        },
        url: {
          add: "/UserIdentity/sysUserIdentity/add",
          edit: "/UserIdentity/sysUserIdentity/edit",
          queryById: "/UserIdentity/sysUserIdentity/queryById",
          getIdentityData:"/UserIdentity/sysUserIdentity/getIdentityData",
        }
      }
    },
    computed: {

      formDisabled(){
        return this.disabled

      },
    },
    created () {
      this.getUser()
      // let _this=this;
      // this.model.userId=_this._props.userId
      //备份model原始值;
      this.modelDefault = JSON.parse(JSON.stringify(this.model));
      // this.getIdentity(_this._props.userId)
    },
    methods: {
      add () {

        this.edit(this.modelDefault);
      },
      getIdentity (userId) {
        getIdentityData(userId).then((res=>{
          console.log(res)
          if (res.result!=null){
          this.model=res.result
            // this.model.userId=res.result.userId
            // this.model.id=res.result.id
            // this.model.businessLicense=res.result.businessLicense
            // this.model.cardFront=res.result.cardFront
            // this.model.cardOpposite=res.result.cardOpposite
            // this.model.enterpriseLogo=res.result.enterpriseLogo
            // this.model.identityType=res.result.identityType
            // this.model.individualDoorstep=res.result.individualDoorstep
            // this.model.realAvatar=res.result.realAvatar
            console.log(this.model)
          }
        }));
      },

      getUser(){
        getUserById().then((res=>{
          this.identityType=res.result.identityType
        }))
      },
      edit (record) {
        this.model = Object.assign({}, record);
        this.visible = true;
      },
      submitForm () {
        const that = this;
        // 触发表单验证
        this.$refs.form.validate(valid => {
          if (valid) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            if(!this.model.id){
              httpurl+=this.url.add;
              method = 'post';
            }else{
              httpurl+=this.url.edit;
               method = 'put';
            }
            httpAction(httpurl,this.model,method).then((res)=>{
              console.log(res)
              if (res.result==1){
                that.$message.warning("您已完成身份认证,不需要再次认证!");
              }else {
                if(res.success){
                  that.$message.success(res.message);
                  that.$emit('ok');
                }else{
                  that.$message.warning(res.message);
                }
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
         
        })
      },
    }
  }
</script>
<style lang="less" scoped>

</style>